# -*- coding: utf-8 -*-
#
# File: Invoice.py
#
# Copyright (c) 2007 by []
# Generator: ArchGenXML Version 1.5.1-svn
#            http://plone.org/products/archgenxml
#
# GNU General Public License (GPL)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

__author__ = """unknown <unknown>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from Products.nurseInvoice.config import *

##code-section module-header #fill in your manual code here
from Products.DataGridField import DataGridField, DataGridWidget
from Products.DataGridField.Column import Column
from Products.DataGridField.SelectColumn import SelectColumn
from Products.Archetypes.public import DisplayList
from Products.Archetypes.public import DateTimeField
from DateTime import DateTime
from Products.nurseInvoice.vocabularies import CODES_PRESTATIONS
##/code-section module-header

schema = Schema((

    ComputedField(
        name='invoiceNurseIdentification',
        widget=ComputedField._properties['widget'](
            label='Invoicenurseidentification',
            label_msgid='nurseInvoice_label_invoiceNurseIdentification',
            i18n_domain='nurseInvoice',
        ),
        expression="context.calculateSomeStuff()"
    ),

##     ComputedField(
##         name='invoiceNurseId',
##         widget=ComputedField._properties['widget'](
##             label='Invoicenurseid',
##             label_msgid='nurseInvoice_label_invoiceNurseId',
##             i18n_domain='nurseInvoice',
##         )
##     ),

    StringField(
        name='invoice',
        widget=StringField._properties['widget'](
            label='Invoice',
            label_msgid='nurseInvoice_label_invoice',
            i18n_domain='nurseInvoice',
        )
    ),

    DataGridField('InvoiceDetails',
                searchable = True,
                columns=("num_titre","code_prestation","date_prestation", "nombre", "executant"),
                         #"date"),
                         #"prestation"),
                         
                         #, "nombre", "brut", "net", "heure", "ppers", "executant"),
                allow_empty_rows = False,
                widget = DataGridWidget(
                    auto_insert = False,     
                    description="Automatically insert new rows when the last row is being filled",
                    columns={
    'num_titre' : Column("num titre"),
    'code_prestation' : SelectColumn("Code prestation", vocabulary="getCodesPrestations", default="sample2"),
    'date_prestation' : SelectColumn("Date prestation", vocabulary="getDatesPrestations"),
    'nombre' : SelectColumn("Nombre", vocabulary="getVocabNombrePrestations", default="1"),
    'executant' : SelectColumn("Executant", vocabulary="getNursesIds"),
                                #default="default2"),

    },
                    ),                 
                  ),

    ),
)

##code-section after-local-schema #fill in your manual code here
##/code-section after-local-schema

Invoice_schema = BaseSchema.copy() + \
    schema.copy()

##code-section after-schema #fill in your manual code here
##/code-section after-schema

class Invoice(BaseContent):
    """
    """
    security = ClassSecurityInfo()
    __implements__ = (getattr(BaseContent,'__implements__',()),)

    # This name appears in the 'add' box
    archetype_name = 'Invoice'

    meta_type = 'Invoice'
    portal_type = 'Invoice'
    allowed_content_types = []
    filter_content_types = 0
    global_allow = 0
    #content_icon = 'Invoice.gif'
    immediate_view = 'base_view'
    default_view = 'base_view'
    suppl_views = ()
    typeDescription = "Invoice"
    typeDescMsgId = 'description_edit_invoice'

    _at_rename_after_creation = True

    schema = Invoice_schema

    ##code-section class-header #fill in your manual code here
    ##/code-section class-header

    # Methods
    def getDatesPrestations(self):
        """
        """
        date = '2005-11-02 13:52:25'
        format = '%d/%m/%Y'
        date_du_jour = DateTime()

        dates = DateTime().strftime(format)
#        import pdb; pdb.set_trace()
        dates_to_display = self.date_components_support(date_du_jour, 0, date_du_jour.year(), date_du_jour.year())
        ## XXX: pas par rapport au mois courant
        display_dates = DisplayList()
### for m in date_du_jour['months']:
        for d in dates_to_display['days']:
            display_dates.add(d['value'] + '/' + repr(date_du_jour.month()) + '/' + repr(date_du_jour.year()),
                              repr(d['id']) + '/' + repr(date_du_jour.month()) + '/' + repr(date_du_jour.year()))
## ##         return DisplayList(
## ##             (("01/01/2004", "01/01/2004",),
##              ("01/01/2005", "01/01/2005",),))
             
        return display_dates
    
    def getCodesPrestations(self):
        """
        returns prestations codes
        """
        codes = DisplayList()
        for dic in CODES_PRESTATIONS:
            codes.add(dic['code'],
                      dic['description'] + "(" + dic['code'] + ")")
            
        return codes
        return DisplayList(
            
            (("sample", "Sample value 1",),
             ("sample2", "Sample value 2",),))
    def calculateSomeStuff(self):
        """
        """
        ## XXX: ne pas calculer pr la date idiot mais pr les 'euros'
        stuff = 0
#        for d in self.InvoiceDetails:
#            stuff += d['date_prestation']
        return "someStuff"
    def getVocabNombrePrestations(self):
        """
        """
        return DisplayList(
                (("1", "1",),
                 ("2", "2",),
                 ("3", "3",),
                 ("4", "4",),
                 ("5", "5",),))

    def getNursesIds(self):
        """
        """
        ## XXX: have to get it from nurse ids defined globally
        return DisplayList(
            (("300744-44", "300744-44",),))
             

registerType(Invoice, PROJECTNAME)
# end of class Invoice

##code-section module-footer #fill in your manual code here
##/code-section module-footer



